package com.igroupes.ovineherd.model.attribute.number;

import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import com.igroupes.ovineherd.entity.DataModelAttributeEntity;
import com.igroupes.ovineherd.enums.BooleanKey;
import com.igroupes.ovineherd.exception.ModelException;
import com.igroupes.ovineherd.model.IParentModelData;
import com.igroupes.ovineherd.model.Style.NumberStyle;
import com.igroupes.ovineherd.model.dto.FieldApiResponse;
import com.igroupes.ovineherd.model.enums.MySqlTypeKey;
import com.igroupes.ovineherd.query.FieldQuery;
import com.igroupes.ovineherd.query.ModelDataJsonAttributeQuery;
import lombok.Data;
import lombok.EqualsAndHashCode;
import lombok.ToString;

import java.util.List;

/**
 * 未知
 */
@EqualsAndHashCode(callSuper = true)
@Data
@ToString(callSuper = true)
public class UnknownNumberModelData extends NumberBasicAttribute {

    public static final String LABEL = "精度";
    public static final String DESC = "为0表示整数,最多填写小数位数";
    private NumberStyle decimal;

    public UnknownNumberModelData() {
        super();
        this.decimal = new NumberStyle(LABEL,0,false,DESC);
    }

    public Boolean verifyField(UnknownNumberModelData unknownNumberModelData) {
        super.verifyField(unknownNumberModelData);
        new NumberStyle().verify(unknownNumberModelData.getDecimal());
        return true;
    }

    public Boolean verifyFieldV2(UnknownNumberModelData basic) {
        super.verifyFieldV2(basic);
        new NumberStyle().verify(new NumberStyle(LABEL,basic.getDecimal().getValue(),false,DESC));
        return true;
    }
    @Override
    public List<FieldQuery> sqlField(IParentModelData parentModelData, DataModelAttributeEntity dataModelAttributeEntityOld) {
        UnknownNumberModelData unknownNumberModelData = (UnknownNumberModelData) parentModelData.getAttributes();
        List<FieldQuery> fieldQueries = super.sqlField(parentModelData,dataModelAttributeEntityOld);
        FieldQuery fieldQuery = fieldQueries.get(0);
        DataModelAttributeEntity dataModelAttributeEntity = fieldQuery.getDataModelAttributeEntity();
        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        jsonObject.set("decimal", Convert.toInt( unknownNumberModelData.getDecimal().getValue()));
        dataModelAttributeEntity.setJson(jsonObject);
        fieldQuery.setPrecision( Convert.toInt( unknownNumberModelData.getDecimal().getValue()));
        fieldQuery.setType(MySqlTypeKey.BIGINT.getCode());
        fieldQuery.setDataModelAttributeEntity(dataModelAttributeEntity);
        fieldQuery.setLength(Convert.toStr(unknownNumberModelData.getMaxValue().getValue()).length()+Convert.toInt( unknownNumberModelData.getDecimal().getValue()));
        fieldQueries.set(0, fieldQuery);
        return fieldQueries;
    }

    @Override
    public JSONObject getAttribute(ModelDataJsonAttributeQuery modelDataJsonAttributeQuery) {
        JSONObject jsonObject = super.getAttribute(modelDataJsonAttributeQuery);
        jsonObject.set("decimal",new NumberStyle(LABEL,modelDataJsonAttributeQuery.getJson().get("decimal"),false,DESC));
        return jsonObject;
    }
    @Override
    public List<FieldApiResponse> verifyRequestData(DataModelAttributeEntity modelAttributeEntity, Object value) {
        List<FieldApiResponse> fieldApiResponses =  super.verifyRequestData(modelAttributeEntity, value);
        if(value == null){
            return fieldApiResponses;
        }
        Integer data = Convert.toInt(value);
        JSONObject jsonObject = modelAttributeEntity.getJson();

        if(ObjectUtil.isNotNull(jsonObject.get("decimal"))) {
            Integer decimal = Convert.toInt(jsonObject.get("decimal"));
            if (doubleyn(value.toString(), decimal)) {
                throw new ModelException(String.format("%s小数位最多个数错误", modelAttributeEntity.getName()));
            }
        }

        if(ObjectUtil.isNotNull(jsonObject.get("minValue"))) {
            Integer minValue = Convert.toInt(jsonObject.get("minValue"));
            if (data < minValue) {
                throw new ModelException(String.format("%s值不能小于%s错误", modelAttributeEntity.getName(), minValue));
            }
        }
        if(ObjectUtil.isNotNull(jsonObject.get("maxValue"))) {
            Integer maxValue = Convert.toInt(jsonObject.get("maxValue"));
            if (data > maxValue) {
                throw new ModelException(String.format("%s值不能大于%s错误", modelAttributeEntity.getName(), maxValue));
            }
        }
        return fieldApiResponses;
    }

    public static boolean doubleyn(String str,int dousize){
        try {
            double num=Double.valueOf(str);//把字符串强制转换为数字
            if(str.trim().indexOf(".") == -1){
                return false;
            }
            int fourplace = str.trim().length() - str.trim().indexOf(".") - 1;
            if(fourplace<dousize){
                return false;
            }else{
                return true;
            }
        } catch (Exception e) {
            return false;//如果抛出异常，返回False
        }
    }
    @Override
    public JSONObject getAttributeStyle(ModelDataJsonAttributeQuery dataModelAttributeEntity) {

        JSONObject jsonObject = dataModelAttributeEntity.getJson();
        Integer minValue = Convert.toInt(jsonObject.get("minValue"));
        Integer maxValue = Convert.toInt(jsonObject.get("maxValue"));
        Integer decimal = Convert.toInt(jsonObject.get("decimal"));
        return new JSONObject(new NumberStyle(
                dataModelAttributeEntity.getName(),"",
                minValue, maxValue, decimal,
                dataModelAttributeEntity.getIsNull().equals(BooleanKey.TRUE.getCode())
        ), false);
    }
}
